<?php
/**  分类故障管理 **/

namespace Admin\Controller;

class CategoryController extends CommonController{

    static public $treelist = array();

    public static function tree($data,$pid=0,$level=0){
        foreach($data as $key=>$value){
            if($value['pid']==$pid){
                $value['level'] = $level;
                self::$treelist[] = $value;
                unset($data[$key]);
                self::tree($data,$value['category_id'],$level+1);
            }
        }
        return self::$treelist;
    }

    protected function _initialize(){
        parent::_initialize();
        $this->user_id=session('user_auth')['uid'];
    }

    /**
     * 一级分类管理
     */
    public function one(){
        $this->breadcrumb1='基础数据管理';
        $this->breadcrumb2='维修';
        $this->breadcrumb3='一级分类管理';
        $this->url2=U('Category/one');
        $menu_id=$_GET['id'];
        $jur=jur_str($menu_id,$this->user_id);
        $name = trim(I('get.name'));
        if(empty($name)){
            $count = M('category')->where('pid = 0')->count();
            $Page  = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
            $show  = $Page->show();// 分页显示输出
            $data = M('category')->where('pid = 0 ')->order(array('order'=>"asc"))->limit($Page->firstRow.','.$Page->listRows)->select();
            $this->assign('page',$show);
        }else{
            $data = M('Category')->where("category_name like '%$name%' and pid = 0")->select();
        }
        $this->assign('category',$data);
        $this->assign('update',$jur['is_update']);// 赋值数据集
        $this->assign('delete',$jur['is_delete']);// 赋值数据集
        $this->display();

    }

    /**
     * 删除分类
     */
    public function del(){
        $id = I('post.Del','');
        $category_id = implode(',',$id);
        $del['category_id'] = array('in',$category_id);
        $maps['pid'] = array('in',$category_id);
        $category_two = M('category')->where($maps)->select();
        $category_fault = M('fault')->where($del)->select();

        if($category_two || $category_fault){
            echo 2;
            exit;
        }
        $data = M('category')->where($del)->delete();
        if($data){
            operatelog('一级分类管理','2',$category_id);
            echo 1;
            exit;
        }else{
            echo -1;
            exit;
        }
    }



    /**
     * 二级分类管理
     */
    public function two(){
        $this->breadcrumb1='基础数据管理';
        $this->breadcrumb2='维修';
        $this->breadcrumb3='二级分类管理';
        $this->url2=U('Category/two');
        $menu_id=$_GET['id'];
        $jur=jur_str($menu_id,$this->user_id);
        $name = trim(I('get.name'));
        if(empty($name)){
            $count = M('category')->where('pid!=0')->count();
            $Page  = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
            $show  = $Page->show();// 分页显示输出
            $data = M('category')->order(array('pid'=>'asc'),array('order'=>'asc'))->limit($Page->firstRow.','.$Page->listRows)->where('pid !=0')->select();
            $this->assign('page',$show);// 赋值分页输出
        }else{
            $data = M('Category')->where("category_name like '%$name%' and pid !=0")->select();
        }

        $this->assign('category',$data);
        $this->assign('update',$jur['is_update']);// 赋值数据集
        $this->assign('delete',$jur['is_delete']);// 赋值数据集
        $this->display();

    }

    /**
     * 故障管理
     */
    public function fault()
    {
        $this->breadcrumb1='基础数据管理';
        $this->breadcrumb2='维修';
        $this->breadcrumb3='故障管理';
        $this->url2=U('Category/fault');
        $menu_id=$_GET['id'];
        $jur=jur_str($menu_id,$this->user_id);

        $name = trim(I('get.name',''));
        $id = I('get.catype','');

        if(!empty($name)){
            $maps['name'] = array('like',"%$name%");
        }

        if(!empty($id) && $id!='0'){
            $maps['category_id'] = array('eq',$id);
        }

        $count = M('fault')->where($maps)->count();
        $Page  = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show  = $Page->show();// 分页显示输出
        $data = M('fault')->where($maps)->order('fault_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        if ($data) {
           foreach($data as $i=>$k){
            $info = M('category')->field('path_name')->where("category_id =" . $data[$i]['category_id'])->select();
            $data[$i]['path_name'] = $info[0]['path_name'];
            if ($data[$i]['fault_type'] == '1') {
                $data[$i]['fault_type'] = '标准';
                $data[$i]['desc'] = '此故障属于标准，显示行业价格';
                $id = $data[$i]['fault_id'];
                $wind = M('fault_cost')->where("fault_id = $id and type = 1")->select();
                $windprice = (int)$wind[0]['material_cost'] + (int)$wind[0]['deplete_cost'] + (int)$wind[0]['artificial_cost'];
                $data[$i]['wind'] = $windprice;
                $standard = M('fault_cost')->where("fault_id = $id and type = 2")->select();
                $standardprice = (int)$standard[0]['material_cost'] + (int)$standard[0]['deplete_cost'] + (int)$standard[0]['artificial_cost'];
                $data[$i]['standard'] = $standardprice;
                $expert = M('fault_cost')->where("fault_id = $id and type = 3")->select();
                $expertprice = (int)$expert[0]['material_cost'] + (int)$expert[0]['deplete_cost'] + (int)$expert[0]['artificial_cost'];
                $data[$i]['expert'] = $expertprice;
            } else {
                $data[$i]['fault_type'] = '非标准';
                $data[$i]['desc'] = '此故障属于非标准，需要诊断';
                $data[$i]['wind'] = '无';
                $data[$i]['expert'] = '无';
                $data[$i]['standard'] = '无';
            }
         }
        }
            $category = M('category')->field('category_id,category_name,path_name')->where("pid != 0")->select();
            $this->assign('category', $category);
            $this->assign('page',$show);
            $this->assign('fault', $data);
        $this->assign('update',$jur['is_update']);// 赋值数据集
        $this->assign('delete',$jur['is_delete']);// 赋值数据集
            $this->display();
    }

    /**
     * 添加故障数据
     */
    public function addfault(){
        if(IS_POST) {
            $data['name'] = trim(I('post.fault_name'));
            $data['order'] = trim(I('post.order'));
            $data['status'] = trim(I('post.status'));
            $data['add_time'] = time();
            $data['category_id'] = trim(I('post.catype'));
            $data['fault_type'] = trim(I('post.type'));
            $return = M('fault')->add($data);

            if ($return) {
                $info['fault_id'] = $return;
                if ($data['fault_type'] == '1') {
                    $info['type'] = trim(I('post.wind'));
                    $info['material_cost'] = trim(I('post.wind_material_cost'));
                    $info['deplete_cost'] = trim(I('post.wind_deplete_cost'));
                    $info['artificial_cost'] = trim(I('post.wind_artificial_cost'));
                    $info['add_time'] = time();
                    M('fault_cost')->add($info);
                }else{
                    $info['type'] = trim(I('post.wind'));
                    $info['material_cost'] = 0;
                    $info['deplete_cost'] = 0;
                    $info['artificial_cost'] = 0;
                    $info['add_time'] = time();
                    M('fault_cost')->add($info);
                }
                if ($data['fault_type'] == '1') {
                    $info['type'] = trim(I('post.expert'));
                    $info['material_cost'] = trim(I('post.expert_material_cost'));
                    $info['deplete_cost'] = trim(I('post.expert_deplete_cost'));
                    $info['artificial_cost'] = trim(I('post.expert_artificial_cost'));
                    $info['add_time'] = time();
                    M('fault_cost')->add($info);
                }else{
                    $info['type'] = trim(I('post.expert'));
                    $info['material_cost'] = 0;
                    $info['deplete_cost'] = 0;
                    $info['artificial_cost'] = 0;
                    $info['add_time'] = time();
                    M('fault_cost')->add($info);
                }
                if ($data['fault_type'] == '1') {
                    $info['type'] = trim(I('post.standard'));
                    $info['material_cost'] = trim(I('post.standard_material_cost'));
                    $info['deplete_cost'] = trim(I('post.standard_deplete_cost'));
                    $info['artificial_cost'] = trim(I('post.standard_artificial_cost'));
                    $info['add_time'] = time();
                    M('fault_cost')->add($info);
                }else{
                    $info['type'] = trim(I('post.standard'));
                    $info['material_cost'] = 0;
                    $info['deplete_cost'] = 0;
                    $info['artificial_cost'] = 0;
                    $info['add_time'] = time();
                    M('fault_cost')->add($info);
                }
                operatelog('故障管理','1',$return);
                echo 1;
                exit;
            } else {
                echo 2;
                exit;
            }
        }
        $this->breadcrumb1='基础数据管理';
        $this->breadcrumb2='维修';
        $this->breadcrumb3='故障管理';
        $this->breadcrumb4='添加';
        $this->url3=U('Category/fault');
        $data = M('category')->order(array('order'=>'desc'))->select();
        $tree = self::tree($data);
        $this->assign('category',$tree);
        $this->display();
    }


    /**
     * 编辑故障信息
     */
    public function editfault()
    {
        if (IS_POST) {
            $fault_id = I('post.fault_id');
            $fault_type = trim(I('post.type'));
            $data['fault_id'] = $fault_id;
            $data['name'] = trim(I('post.fault_name'));
            $data['category_id'] = trim(I('post.catype'));
            $data['order'] = trim(I('post.order'));
            $data['fault_type'] = $fault_type;
            $data['status'] = trim(I('post.status'));
            $fault = M('fault')->save($data);
            if ($fault_type == '1') {
                $map['fault_id'] = array('eq',$fault_id);
                M('fault_cost')->where($map)->delete();

                $inf['fault_id'] = $fault_id;
                $inf['add_time'] = time();
                $inf['type'] = '1';
                $inf['material_cost'] = trim(I('post.wind_material_cost'));
                $inf['deplete_cost'] = trim(I('post.wind_deplete_cost'));
                $inf['artificial_cost'] = trim(I('post.wind_artificial_cost'));
                $wind = M('fault_cost')->add($inf);

                $inf['type'] = '2';
                $inf['material_cost'] = trim(I('post.standard_material_cost'));
                $inf['deplete_cost'] = trim(I('post.standard_deplete_cost'));
                $inf['artificial_cost'] = trim(I('post.standard_artificial_cost'));
                $standard = M('fault_cost')->add($inf);

                $inf['type'] = '3';
                $inf['material_cost'] = trim(I('post.expert_material_cost'));
                $inf['deplete_cost'] = trim(I('post.expert_deplete_cost'));
                $inf['artificial_cost'] = trim(I('post.expert_artificial_cost'));
                $expert = M('fault_cost')->add($inf);

                if($fault || $wind &$standard &$expert){
                    operatelog('故障管理','3',$fault_id);
                    echo 1;
                    exit;
                }else{
                    echo 2;
                    exit;
                }

                $map['type'] = array('eq',1);
                $inf['material_cost'] = trim(I('post.wind_material_cost'));
                $inf['deplete_cost'] = trim(I('post.wind_deplete_cost'));
                $inf['artificial_cost'] = trim(I('post.wind_artificial_cost'));
                $wind = M('fault_cost')->where($map)->save($inf);

                $map['type'] = array('eq',2);
                $inf['material_cost'] = trim(I('post.standard_material_cost'));
                $inf['deplete_cost'] = trim(I('post.standard_deplete_cost'));
                $inf['artificial_cost'] = trim(I('post.standard_artificial_cost'));
                $standard = M('fault_cost')->where($map)->save($inf);

                $map['type'] = array('eq',3);
                $inf['material_cost'] = trim(I('post.expert_material_cost'));
                $inf['deplete_cost'] = trim(I('post.expert_deplete_cost'));
                $inf['artificial_cost'] = trim(I('post.expert_artificial_cost'));
                $expert = M('fault_cost')->where($map)->save($inf);

                if(!$fault & !$wind &!$standard &!$expert) {
                    echo 2;
                    exit;
                }else{
                    operatelog('故障管理','3',$fault_id);
                    echo 1;
                    exit;
                }

            } else {
                $faultcost = M('fault_cost')->where("fault_id = $fault_id")->delete();

                $inf['fault_id'] = $fault_id;
                $inf['add_time'] = time();
                $inf['material_cost'] = 0;
                $inf['deplete_cost'] = 0;
                $inf['artificial_cost'] = 0;
                $inf['type'] = '1';
                $wind = M('fault_cost')->add($inf);

                $inf['type'] = '2';
                $standard = M('fault_cost')->add($inf);

                $inf['type'] = '3';
                $expert = M('fault_cost')->add($inf);
                if(!$faultcost && !$wind && !$standard && !$expert ) {
                    echo 2;
                    exit;
                }else{
                    operatelog('故障管理','3',$fault_id);
                    echo 1;
                    exit;
                }
            }

        }

        $id = I('get.id');
        $info = M('fault')->field('name,order,status,fault_type,fault_id,category_id')->where("fault_id = $id")->select();
        $data = M('category')->order(array('order' => 'desc'))->select();
        if($info[0]['fault_type']=='1') {
            $cost = M('fault_cost')->where("fault_id = $id")->select();
            foreach ($cost as $k => $v) {
                if ($cost[$k][type] == '1') {
                    $info[0]['wind_material_cost'] = $cost[$k]['material_cost'];
                    $info[0]['wind_deplete_cost'] = $cost[$k]['deplete_cost'];
                    $info[0]['wind_artificial_cost'] = $cost[$k]['artificial_cost'];
                }
                if ($cost[$k][type] == '2') {
                    $info[0]['standard_material_cost'] = $cost[$k]['material_cost'];
                    $info[0]['standard_deplete_cost'] = $cost[$k]['deplete_cost'];
                    $info[0]['standard_artificial_cost'] = $cost[$k]['artificial_cost'];
                }
                if ($cost[$k][type] == '3') {
                    $info[0]['expert_material_cost'] = $cost[$k]['material_cost'];
                    $info[0]['expert_deplete_cost'] = $cost[$k]['deplete_cost'];
                    $info[0]['expert_artificial_cost'] = $cost[$k]['artificial_cost'];
                }
            }
        }
        $tree = self::tree($data);
        $this->breadcrumb1='基础数据管理';
        $this->breadcrumb2='维修';
        $this->breadcrumb3='故障管理';
        $this->breadcrumb4='编辑';
        $this->url3=U('Category/fault');
        $this->assign('info', $info);
        $this->assign('category', $tree);
        $this->display();
    }


    /**
     * 删除故障信息
     */
    public function delfault(){
        $id = I('post.fault');
        $maps['fault_id'] =array('in',$id);
        $data = M('fault')->where($maps)->delete();
        if($data){
            M('fault_cost')->where($maps)->delete();
            operatelog('故障管理','2',$id);
            echo 1;
            exit;
        }else{
            echo -1;
            exit;
        }
    }

    /**
     * 添加一级分类数据
     */
    public function addone(){
        if(IS_POST) {
            $upload = new \Think\Upload(C('UPLOAD_CONFIG'));
            $info = $upload->upload($_FILES);
            if (!$info) {// 上传错误提示错误信息
//                $data = array(code => 403, msg => $upload->getError());
//                $this->ajaxReturn($data, 'JSON');
                echo -1;
                exit;
            }
            $info['icon'] = $info[icon][url];
            $info['category_name'] = trim(I('post.category_name'));
            $info['desc'] = trim(I('post.desc'));
            $info['order'] = trim(I('post.order'));
            $data = M('category')->add($info);
            if ($data) {
                operatelog('一级分类管理','1',$data);
                echo 1;
                exit;
            } else {
                echo 2;
                exit;
            }
        }
        $this->breadcrumb1='基础数据管理';
        $this->breadcrumb2='维修';
        $this->breadcrumb3='一级分类管理';
        $this->breadcrumb4='添加';
        $this->url3=U('Category/one');
        $this->display();
    }

    /**
     * 编辑一级分类
     */
    public function edit(){
        if(IS_POST){
            $upload = new \Think\Upload(C('UPLOAD_CONFIG'));
            $info = $upload->upload($_FILES);
            if ($info) {// 上传错误提示错误信息
                $data['icon'] = $info[icon][url];
            }
            $category_id = I('post.id');
            $category_name = trim(I('post.category_name'));
            $data['category_id'] = $category_id;
            $data['category_name'] = $category_name;
            $data['desc'] = trim(I('post.desc'));
            $data['order'] = trim(I('post.order'));
            $data['status'] = trim(I('post.status'));
            $return = M('category')->save($data);
            if((int)$return){
                if($category_name){
                    $category_two = M('category')->field('category_name,category_id')->where("pid = $category_id")->select();
                    foreach($category_two as $k=>$v){
                        $maps['path_name'] = "$category_name | ".$category_two[$k]['category_name'];
                        $whe['category_id'] = $category_two[$k]['category_id'];
                        $whe['pid'] = array('neq','0');
                        M('category')->where($whe)->save($maps);
                    }
                }
                operatelog('一级分类管理','3',$category_id);
                echo 1;
                exit;
            }else{
                echo 2;
                exit;
            }
        }
        $id = I('get.id');
        $this->breadcrumb1='基础数据管理';
        $this->breadcrumb2='维修';
        $this->breadcrumb3='一级分类管理';
        $this->breadcrumb4='编辑';
        $this->url3=U('Category/one');
        $data = M('category')->where('category_id='.$id)->select();
        $this->assign('editcategory',$data);
        $this->display();
    }



    /**
     * 添加二级分类数据
     */
    public function addtwo(){
        if(IS_POST){
            $upload = new \Think\Upload(C('UPLOAD_CONFIG'));
            $info = $upload->upload($_FILES);
            if (!$info) {// 上传错误提示错误信息
//                $data=array(code=>403,msg=>$upload->getError());
//                $this->ajaxReturn($data,'JSON');
                echo -1;
                exit;
            }
            $data['icon'] = $info[icon][url];
            $data['category_name'] = trim(I('post.category_name'));
            $data['desc'] = trim(I('post.desc'));
            $data['order'] = trim(I('post.order'));
            $data['status'] = trim(I('post.status'));
            $data['pid'] = trim(I('post.type'));
            $data['dia_standard_cost'] = trim(I('post.standard'));
            $data['dia_expert_cost'] = trim(I('post.expert'));
            $data['dia_wind_cost'] =trim(I('post.wind'));
            $arry = explode(',',$data['pid']);
            $data['path_name'] =$arry[1].' | '.$data['category_name'];
            $data['pid'] = $arry[0];
            $add =M('category')->add($data);
            if($add){
                operatelog('二级分类管理','1',$add);
                $catid = M('category')->field('category_id')->order('category_id desc')->find();
                $categoryid = $catid['category_id'];
                $sql = "INSERT INTO zsf_agreement VALUES (null,'周',1,$categoryid,40,60,0),(null,'月',1,$categoryid,30,70,0),(null,'年',1,$categoryid,20,80,0)";
                $model = M();
                $model->execute($sql);
                echo 1;
                exit;
            }else{
                echo 2;
                exit;
            }
        }
        $this->breadcrumb1='基础数据管理';
        $this->breadcrumb2='维修';
        $this->breadcrumb3='二级分类管理';
        $this->breadcrumb4='添加';
        $this->url3=U('Category/two');
        $data =M('category')->field('category_id,category_name')->where('pid = 0')->select();
        $this->assign('select',$data);
        $this->display();
    }


    /**
     * 编辑二级分类
     */
    public function edittwo(){
        if(IS_POST){
            $upload = new \Think\Upload(C('UPLOAD_CONFIG'));
            $info = $upload->upload($_FILES);
            if ($info) {// 上传错误提示错误信息
                $data['icon'] = $info[icon][url];
            }
            $category_id = I('post.id');
            $data['category_id'] = $category_id;
            $data['category_name'] = trim(I('post.category_name'));
            $data['desc'] = trim(I('post.desc'));
            $data['order'] = trim(I('post.order'));
            $data['status'] = trim(I('post.status'));
            $data['pid'] = trim(I('post.type'));
            $data['dia_standard_cost'] = trim(I('post.standard'));
            $data['dia_expert_cost'] = trim(I('post.expert'));
            $data['dia_wind_cost'] = trim(I('post.wind'));
            $arry = explode(',',$data['pid']);
            $data['pid'] = $arry[0];
            $data['path_name'] =$arry[1].' | '.$data['category_name'];
            $return = M('category')->save($data);
            if((int)$return){
                operatelog('一级分类管理','3',$category_id);
                echo 1;
                exit;
            }else{
                echo 2;
                exit;
            }
        }
        $id = I('get.id');
        $this->breadcrumb1='基础数据管理';
        $this->breadcrumb2='维修';
        $this->breadcrumb3='二级分类管理';
        $this->breadcrumb4='编辑';
        $this->url3=U('Category/two');
        $data =M('category')->field('category_id,category_name')->where('pid = 0')->select();
        $info =M('category')->where('category_id='.$id)->select();
        $this->assign('edittwo',$info);
        $this->assign('select',$data);
        $this->display();
    }

}